<template>
	<div class="login-container">
  <div class="login">
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" label-position="left" label-width="0px" class="login-form">
      <h3 class="title">
        后台管理系统
      </h3>
      <el-form-item prop="username">
        <el-input v-model="loginForm.username" type="text" auto-complete="off" placeholder="账号">
          <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" />
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input v-model="loginForm.password" type="password" auto-complete="off" placeholder="密码" @keyup.enter.native="handleLogin">
          <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" />
        </el-input>
      </el-form-item>
	  <el-form-item prop="code">
	  				  <el-input
	  				    v-model="loginForm.code"
	  				    auto-complete="off"
	  				    placeholder="验证码"
	  				    style="width: 63%"
	  				    @keyup.enter.native="handleLogin"
	  				  >
	  				    <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" />
	  				  </el-input>
	  				  <div class="login-code">
	  				    <img :src="codeUrl" @click="getCode" />
	  				  </div>
	  </el-form-item>
      <el-form-item>
        <el-button :loading="loading" size="medium" type="primary" style="width:60%;border-radius: 30px;text-align: center;" @click.native.prevent="handleLogin">
          <span v-if="!loading">登 录</span>
          <span v-else>登 录 中...</span>
        </el-button>
		</el-form-item>
    </el-form>
	<div class="tips" style="margin-top: 20px;">
	  <div style="text-align: center;">
	    <svg-icon icon-class="qq" style="width: 100px;height: 50px;" />
	    <svg-icon icon-class="wxchat" style="width: 100px;height: 50px;margin-left: 50px;" />
	  </div>
	</div>
  </div>
</div>
</template>

<script>
import { getCodeImg } from "@/api/login";
	
export default {
  name: 'Login',
  data() {
    return {
      codeUrl: '',
      cookiePass: '',
      loginForm: {
        username: 'admin',
        password: 'admin',
		code: '',
		uuid: ''
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', message: '用户名不能为空' }],
        password: [{ required: true, trigger: 'blur', message: '密码不能为空' }],
        code: [{ required: true, trigger: 'change', message: '验证码不能为空' }]
      },
      loading: false,
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  created() {
		this.getCode()
  },
  methods: {
	getCode() {
		getCodeImg().then(res => {
		  this.codeUrl = "data:image/gif;base64," + res.img;
		  this.loginForm.uuid = res.uuid
		})
	},
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('Login', this.loginForm).then(() => {
            this.loading = false
            this.$router.push({ path: this.redirect || '/' })
          }).catch(() => {
            this.loading = false
			this.getCode()
          })
        } else {
          console.log('error submit!!')
          return false
        }
      })
    }
  }
}
</script>
<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg:#283443;
$light_gray:#fff;
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
	display: flex;
	justify-content: center;
	align-items: center;
	height: 100%;
	background-image:url(../assets/images/th.jpg);
	background-size: cover;
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      color: $light_gray;
      height: 47px;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .login
  {
    box-shadow: -15px 15px 15px rgba(6, 17, 47, 0.7);
    opacity: 1;
    top: 20px;
    -webkit-transition-timing-function: cubic-bezier(0.68, -0.25, 0.265, 0.85);
    -webkit-transition-property: -webkit-transform,opacity,box-shadow,top,left;
            transition-property: transform,opacity,box-shadow,top,left;
    -webkit-transition-duration: .5s;
            transition-duration: .5s;
    -webkit-transform-origin: 161px 100%;
        -ms-transform-origin: 161px 100%;
            transform-origin: 161px 100%;
    -webkit-transform: rotateX(0deg);
            transform: rotateX(0deg);
    position: relative;
    width: 390px;
    /*border-top: 2px solid #D8312A;*/
    height: 500px;
    position: absolute;
    left: 0;
    right: 0;
	border-radius: 10px;
    margin: auto;
    top: 0;
    bottom: 0;
    padding: 80px 40px 40px 40px;
    background: #35394a;
    /* Old browsers */
    /* FF3.6+ */
    background: -webkit-gradient(linear, left bottom, right top, color-stop(0%, #35394a), color-stop(100%, rgb(0, 0, 0)));
    /* Chrome,Safari4+ */
    background: -webkit-linear-gradient(230deg, rgba(53, 57, 74, 0) 0%, rgb(0, 0, 0) 100%);
    /* Chrome10+,Safari5.1+ */
    /* Opera 11.10+ */
    /* IE10+ */
    background: linear-gradient(230deg, rgba(53, 57, 74, 0) 0%, rgb(0, 0, 0) 100%);
    /* W3C */
    filter: progid:DXImageTransform.Microsoft.gradient( startColorstr='rgba(53, 57, 74, 0)', endColorstr='rgb(0, 0, 0)',GradientType=1 );
    /* IE6-9 fallback on horizontal gradient */
  }

}
</style>
<style rel="stylesheet/scss" lang="scss">
  .title {
    color: #FFFFFF;
  }

  .login-form {
    border-radius: 6px;
    padding: 25px 25px 5px 25px;
    .el-input {
      height: 38px;
      input {
        height: 38px;
      }
    }
    .input-icon{
      height: 39px;width: 24px;
    }
  }
  .login-tip {
    font-size: 13px;
    text-align: center;
    color: #bfbfbf;
  }
  .login-code {
    width: 33%;
    display: inline-block;
    height: 38px;
    float: right;
    img{
      cursor: pointer;
      vertical-align:middle
    }
  }
</style>
